
app.chrome = {}

app.chrome.ChromeView = Backbone.View.extend({
	
	initialize: function () {},
	
	template: _.template( app.get("tpl/chrome/chrome.html") ),
	
	render: function () {
		var view = this;
		$.ajax({
			"type":"POST",
			"url" : "api/login"
		}).error(function(response){
			app.admin.user = null;
			$(view.el).html(view.template());
			Backbone.history.loadUrl( Backbone.history.fragment , { trigger: true } );
		}).done(function(response){
			app.admin.user = new app.admin.User(response);
			$(view.el).html(view.template());
			Backbone.history.loadUrl( Backbone.history.fragment , { trigger: true } );
		});
		return this;
	},
	
    doLogin : function() {
		var view = this;
		$.ajax({
			"type":"POST",
			"url" : "api/login",
			"data":{
				"email"		:	$("#email").val(),
				"password"	:	$("#password").val()
			}
		}).error(function(response){
			app.admin.user = null;
			$(view.el).html(view.template());
			Backbone.history.loadUrl( Backbone.history.fragment , { trigger: true } );
		}).done(function(response){
			app.admin.user = new app.admin.User(response);
			$(view.el).html(view.template());
			Backbone.history.loadUrl( Backbone.history.fragment , { trigger: true } );
		});
    },
    
    doLogout : function() {
		var view = this;
		$.ajax({
			"type":"POST",
			"url" : "api/logout"
		}).always(function(response){
			app.admin.user = null;
			$(view.el).html(view.template());
			app.router.navigate( "" , { trigger: true } );
		});
    },
    
    updateMenu: function () {
		var fragment = Backbone.history.fragment == "" ? "home" : Backbone.history.fragment;
		$("#navbar li", this.el).each(function(){
			$(this).hasClass(fragment) ? $(this).addClass("active") : $(this).removeClass("active");
		});
	},
	
    events: {
		"click button.login" : "doLogin",
		"click li.logout" : "doLogout"
    },
    
});